package cn.jlonely.xyhx.admin.service.impl;

import cn.jlonely.xyhx.admin.mapper.VitoMapper;
import cn.jlonely.xyhx.admin.service.VitoService;
import cn.jlonely.xyhx.pojo.VitoExcelData;
import cn.jlonely.xyhx.pojo.vo.ReviewVo;
import com.alibaba.excel.EasyExcel;
import io.jsonwebtoken.io.IOException;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.List;

@Service
public class VitoServiceImpl implements VitoService {
    @Autowired
    private VitoMapper vitoMapper;

    /**
     * 获取某个活动排名
     *
     * @param id
     * @return
     */
    @Override
    public List<ReviewVo> getVitoInfo(Integer id) {
        return vitoMapper.getVitoInfo(id);
    }

    /**
     * 导出Excel
     * @param response
     * @param id
     * @throws IOException
     */

    public void exportToExcel(HttpServletResponse response, Integer id) throws java.io.IOException {
        // 1. 查询数据
        List<VitoExcelData> vitoList = vitoMapper.getVitoList(id);

        // 2. 设置 Excel 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("数据列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");

        // 3. 写入 Excel
        EasyExcel.write(response.getOutputStream(), VitoExcelData.class)
                .sheet("Sheet1")
                .doWrite(vitoList);
    }
}
